<?php

namespace app\api\model;

require_once ROOT_PATH.'extend/QRcode.php';

use app\common\library\Auth;
use extend\QRcode as QRcodeExtend;
use think\Model;
use think\Request;

class Qrcode extends Model
{

    //小程序码
    public function createWxQrcode(array $param)
    {
        try {

            $checkpath_result = $this->checkPath($param['qrcode_path']);

            if ($checkpath_result["code"] != 1) return $checkpath_result;

            // scene:场景值最大32个可见字符，只支持数字，大小写英文以及部分特殊字符：!#$&'()*+,/:;=?@-._~
            $scene = '';
            if (!empty($param['data'])) {
                foreach ($param['data'] as $key => $value) {
                    if ($scene == '') $scene .= $key . '=' . $value;
                    else $scene .= '&' . $key . '=' . $value;
                }
            }
            $page = substr($param['page'], 1);
            $width = isset($param['width']) ? $param['width'] : 120;
            $auth = new Auth();
            $response = $auth->qrcode($scene,$page,$width,$param['qrcode_path'],$param['qrcode_name']);

            if (!$response){
                return [
                    'code'=>-1,
                    'msg'=>'小程序码生成失败'
                ];
            } else {
                return [
                    'code'=>0,
                    'data'=>[
                        'path'=>$response
                    ]
                ];
                /*if ($response instanceof \EasyWeChat\Kernel\Http\StreamResponse) {
                    $filename = $param['qrcode_path'] . '/';
                    $filename .= $response->saveAs($param['qrcode_path'], $param['qrcode_name'] . '_' . $param['app_type'] . '.png');

                    return [
                        'code'=>0,
                        'data'=>[
                            'path'=>$response
                        ]
                    ];
                }*/

            }

            return [
                'code'=>-1,
                'msg'=>''
            ];

        } catch (\Exception $e) {
            return [
                'code'=>-1,
                'msg'=>$e->getMessage()
            ];
        }
    }

    //二维码
    public function createQrcode(array $param)
    {
        try {
            $checkpath_result = $this->checkPath($param['qrcode_path']);

            if($checkpath_result["code"] != 1) return $checkpath_result;

            $urlParam = '';
            if (!empty($param['data'])) {
                foreach ($param['data'] as $key => $value) {
                    if ($urlParam == '') $urlParam .= '?' . $key .'='. $value;
                    else $urlParam .= '&' . $key .'='. $value;
                }
            }

            $request = Request::instance();
            $domain=$request->domain();
            //$url = $domain . '/'.$param['page'] . $urlParam;
            $url =  $domain . $urlParam;
            $filename = $param['qrcode_path'] . '/' . $param['qrcode_name'] . '_' . $param['app_type'] . '.png';

            QRcodeExtend::png($url, $filename, 'L', 4, 1);
            return [
                'code'=>1,
                'data'=>[
                    'path'=>$filename
                ]
            ];
            //return $filename;
        } catch (\Exception $e) {
            return [
                'code'=>-1,
                'msg'=>$e->getMessage()
            ];
            //return $this->getError($e->getMessage());
        }
    }


    //二维码不带域名
    public function createQrcode2(array $param)
    {
        try {
            $checkpath_result = $this->checkPath($param['qrcode_path']);

            if($checkpath_result["code"] != 1) return $checkpath_result;

            $urlParam = '';
            if (!empty($param['data'])) {
                foreach ($param['data'] as $key => $value) {
                    if ($urlParam == '') $urlParam .= '?' . $key .'='. $value;
                    else $urlParam .= '&' . $key .'='. $value;
                }
            }

            $request = Request::instance();
            $domain=$request->domain();
            $url = $domain . '/collection' . $urlParam;
            //$url =    $url.$urlParam;

            $filename = $param['qrcode_path'] . '/' . $param['qrcode_name'] . '_' . $param['app_type'] . '.png';

            QRcodeExtend::png($url, $filename, 'L', 4, 1);
            return [
                'code'=>1,
                'data'=>[
                    'path'=>$filename
                ]
            ];
            //return $filename;
        } catch (\Exception $e) {
            return [
                'code'=>-1,
                'msg'=>$e->getMessage()
            ];
            //return $this->getError($e->getMessage());
        }
    }

    /**
     * 校验目录是否可写
     * @param unknown $path
     * @return multitype:number unknown |multitype:unknown
     */
    private function checkPath($path)
    {
        if (is_dir($path) || mkdir($path, intval('0755', 8), true)) {
            return [
                'code'=>1
            ];
        }
        return [
            'code'=>0,
            'msg'=>"directory {$path} creation failed"
        ];
        //return $this->getError('', "directory {$path} creation failed");
    }

}